package com.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uaa.dao.UserQueryDao;
import entity.LoginUser;
import entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 验证用户登录
 *
 * @author Daft_V
 * @date 2022/5/19 15:30
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserQueryDao userQueryDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("username", username);
        // 根据用户名查询用户
        UserEntity userEntity = userQueryDao.getBaseMapper().selectOne(userEntityQueryWrapper);
        log.info(userEntity.toString());

        if (Objects.isNull(userEntity)) {
            throw new RuntimeException("用户名或密码错误");
        }

        List<String> permissions=new ArrayList<>();
        permissions.add("orderList");
        return new LoginUser(userEntity,permissions);
    }
}
